package com.doctorliang.cheng.shopping.type.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.type.bean.TagBean;

import java.util.List;

/**
 * Created by YBH on 2017/12/14.
 */

public class TagGridViewAdapter extends BaseAdapter {
    private  Context mContext;
    private  List<TagBean.ResultBean> result;
    //随机颜色
    private int[] colors = {Color.parseColor("#f0a420"), Color.parseColor("#4ba5e2"), Color.parseColor("#f0839a"),
            Color.parseColor("#4ba5e2"), Color.parseColor("#f0839a"), Color.parseColor("#f0a420"),
            Color.parseColor("#f0839a"), Color.parseColor("#f0a420"), Color.parseColor("#4ba5e2")};

    public TagGridViewAdapter(Context mcontext, List<TagBean.ResultBean> result) {
        this.mContext = mcontext;
        this.result = result;
    }

    @Override
    public int getCount() {
        return result==null?0:result.size();
    }

    @Override
    public Object getItem(int position) {
        return result.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_tab_gridview, null);
            viewHolder = new ViewHolder();
            viewHolder.tvTag = convertView.findViewById(R.id.tv_tag);
            convertView.setTag(viewHolder);
        } else {
             viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvTag.setText(result.get(position).getName());
        viewHolder.tvTag.setTextColor(colors[position % colors.length]);
        return convertView;
    }


    static  class ViewHolder{
        TextView tvTag;
    }
}
